﻿// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

// ReSharper disable CheckNamespace
// ReSharper disable CommentTypo
// ReSharper disable IdentifierTypo

/* SortCommand.cs -- установка сортировки
 * Ars Magna project, http://arsmagna.ru
 */

#region Using directives

using System.Linq;

using AM;

using ManagedIrbis.Infrastructure;

using SyncProviderUtility = ManagedIrbis.Providers.SyncProviderUtility;

#endregion

#nullable enable

namespace ManagedIrbis.Mx.Commands;

/// <summary>
/// Установка сортировки.
/// </summary>
public sealed class SortCommand
    : MxCommand
{
    #region Construction

    /// <summary>
    /// Конструктор по умолчанию.
    /// </summary>
    public SortCommand()
        : base ("sort")
    {
        // пустое тело конструктора
    }

    #endregion

    #region MxCommand members

    /// <inheritdoc cref="MxCommand.Execute" />
    public override bool Execute
        (
            MxExecutive executive,
            MxArgument[] arguments
        )
    {
        OnBeforeExecute();

        string? argument = null;
        if (arguments.Length != 0)
        {
            argument = arguments[0].Text;
        }

        if (string.IsNullOrEmpty (argument))
        {
            var sort = executive.OrderFormat;
            if (string.IsNullOrEmpty (sort))
            {
                sort = "OFF";
            }

            executive.WriteMessage ($"SORT is: {sort}");
        }
        else if (argument.SameString ("off"))
        {
            executive.OrderFormat = null;
            executive.WriteMessage ("SORT is OFF now");
        }
        else
        {
            executive.OrderFormat = argument;
        }

        OnAfterExecute();

        return true;
    }

    #endregion
}
